package com.kyzh.core.adapters

import android.animation.ObjectAnimator
import android.view.View
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.SmallAccount
import com.gushenge.core.beans.SmallAccountGame
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemSmallAccountItem1Binding
import com.kyzh.core.databinding.ItemSmallAccountItem2Binding
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.dip
import com.gushenge.core.toast

class SmallAccountManagerAdapter:BaseNodeAdapter() {
    init {
        addNodeProvider(SmallAccountGameProvider())
        addNodeProvider(SmallAccountProvider())
    }
    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return if (data[position] is SmallAccountGame) 0 else 1
    }
}

class SmallAccountGameProvider : BaseNodeProvider(){
    override val itemViewType: Int
        get() = 0

    override val layoutId: Int
        get() = R.layout.item_small_account_item1

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind<ItemSmallAccountItem1Binding>(viewHolder.itemView)
    }
    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        helper.getBinding<ItemSmallAccountItem1Binding>()?.data = item as SmallAccountGame
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {

        val start = helper.getView<ImageView>(R.id.ivArrow)
        if ((data as SmallAccountGame).small.isEmpty()) {
            context.getString(R.string.noSmallInThisGame).toast()
        } else {
            if (data.isExpanded) {
                getAdapter()?.collapse(position,false)
                ObjectAnimator.ofFloat(start, "rotation", 0f).setDuration(500).start()
            } else {
                getAdapter()?.expand(position,false)
                ObjectAnimator.ofFloat(start, "rotation", 90f).setDuration(500).start()
            }
        }
    }
}
class SmallAccountProvider : BaseNodeProvider(){
    override val itemViewType: Int
        get() = 1

    override val layoutId: Int
        get() = R.layout.item_small_account_item2

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind<ItemSmallAccountItem2Binding>(viewHolder.itemView)
    }
    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        helper.getBinding<ItemSmallAccountItem2Binding>()?.let {binding->
            binding.data = item as SmallAccount
            getAdapter()?.let {
                val bean = it.getItem(it.findParentNode(item)) as SmallAccountGame
                if (bean.small[bean.small.size-1].role_name ==item.role_name){
                    binding.ivBg.setCornerRadius(0f,0f,context.dip(10).toFloat(),context.dip(10).toFloat())
                    binding.v1.visibility = View.VISIBLE
                }else{
                    binding.ivBg.setCornerRadius(0f,0f,0f,0f)
                    binding.v1.visibility = View.GONE
                }

                binding.tvSelect.setOnClickListener {v->
                    UserRequest.setMainSmallAccount(item.id){
                        UserRequest.getSmallAccountList {
                            /*for (i in 0 until this.size){
                                Log.e("convert: ",i.toString() )
                                it.nodeReplaceChildData(it.getItem(i ),this[i].small)
                            }*/
                            it.setList(this)
                        }
                    }

                }
                binding.tvDelete.setOnClickListener {v->
                    context.alert(
                            context.getString(R.string.deleteSmall1),
                            context.getString(R.string.deleteSmall1),
                            context.getString(R.string.sure),
                            context.getString(R.string.cancel),
                            {
                                UserRequest.smallDel(item.uid){
                                    UserRequest.getSmallAccountList {
                                        /*for (i in 0 until this.size){
                                            Log.e("convert: ",i.toString() )
                                            it.nodeReplaceChildData(it.getItem(i ),this[i].small)
                                        }*/
                                        it.setList(this)
                                    }
                                }
                            },
                            {

                            }
                    )
                }
            }
        }

    }

}